import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class test {
    /**
    String className;

    //先从配置文件中取出要创建的类名
    @Before
    public void init() throws IOException {
        //1.加载配置文件
        //1.1创建properties对象
        Properties myapp = new Properties();
        //1.2加载配置文件转换为一个集合
        //1.2.1获取class目录下的配置文件
        ClassLoader cl = demo.class.getClassLoader();//类加载器
        InputStream is = cl.getResourceAsStream("myapp.properties");
        myapp.load(is);

        //2.获取配置文件中定义的数据
        className = myapp.getProperty("bootstrapClass");
        //System.out.println(className);
    }

    //成功创建类
    @Test
    public void createClass1() throws Exception {
        Class cls = Class.forName(className);
        cls.newInstance();
    }

    //类不存在
    @Test
    public void createClass2() throws Exception {
        Class cls = Class.forName("test1");
        cls.newInstance();
    }

    @Test
    public void testMethod() throws Exception {
        Class cls = Class.forName(className);
        MyClass myclass1 = (MyClass) cls.newInstance();
        //找到有注解的方法
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            if (m.isAnnotationPresent(InitMethod.class)) {
                //返回正确结果
                Assert.assertEquals(3, m.invoke(myclass1, 1, 2));
                Assert.assertEquals(5, m.invoke(myclass1, 3, 2));
            }
        }
    }

    @Test
    public void testMethod2() throws Exception {
        Class cls = Class.forName(className);
        MyClass myclass2 = (MyClass) cls.newInstance();
        //找到有注解的方法
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            if (m.isAnnotationPresent(InitMethod.class)) {
                //有参数无法运行
                Assert.assertEquals(3, m.invoke(myclass2, 1, 1.5));
            }
        }
    }
    */
    private demo2 demoTest;
    private String className;
    @Before
     public void init() throws IOException {
        demoTest = new demo2();
        className=demoTest.read();
    }

    //成功创建
    @Test
    public void classTest1() throws Exception {
        Object obj = demoTest.createClass(className);
        Assert.assertEquals(MyClass.class,obj.getClass());
    }
    //类不存在
    @Test
    public void classTest2() throws Exception {
        Object obj = demoTest.createClass("balabala");
        Assert.assertEquals(MyClass.class,obj.getClass());
    }
    @Test
    public void methodTest() throws Exception {
        Object obj=demoTest.createClass(className);
        Method m=demoTest.method(className);

        Assert.assertEquals(1,m.invoke(obj,0,1));
        Assert.assertEquals(3,m.invoke(obj,1,2));
    }

    //有参数无法运行
    @Test
    public void methodTest2() throws Exception {
        Object obj=demoTest.createClass(className);
        Method m=demoTest.method(className);

        Assert.assertEquals(2.5,m.invoke(obj,1.5,1));
    }
}